package com.kerrykidz.system.util.sync;

import java.io.Serializable;
import java.util.List;

public class SyncBean implements Serializable {

	public static final String COMMON_SQL = "SELECT * FROM $ WHERE & BETWEEN ? AND ?";

	public static final String NOT_COMMON_SQL = "SELECT * FROM $ WHERE ";

	private static final long serialVersionUID = 1L;
	public static final int SYNCTYPE_MD = 1; // 只允许门店同步到总部
	public static final int SYNCTYPE_ZB = 2; // 只允许总部同步到门店
	public static final int SYNCTYPE_MD_ZB = 3; // 允许两边相互同步

	/**
	 * 主表的构造
	 * 
	 * @param tableName
	 * @param primary
	 * @param syncType
	 */
	public SyncBean(String tableName, String primary, int syncType) {
		this.tableName = tableName;
		this.sql = COMMON_SQL.replace("$", tableName).replace("&", SqlHelper.CONDITION_MODI_TIME);
		this.primary = primary;
		this.syncType = syncType;
	}

	/**
	 * 主表的构造
	 * 
	 * @param tableName
	 * @param primary
	 * @param syncType
	 */
	public SyncBean(String tableName, String primary, int syncType, OrderBy orderby) {
		this.tableName = tableName;
		this.sql = COMMON_SQL.replace("$", tableName).replace("&", SqlHelper.CONDITION_MODI_TIME);
		if (orderby != null)
			sql += " ORDER BY " + orderby.getColumn() + " " + orderby.getMode();
		this.primary = primary;
		this.syncType = syncType;
	}
	
	/**
	 * 主表的构造
	 * 
	 * @param tableName
	 * @param primary
	 * @param syncType
	 */
	public SyncBean(String tableName, String primary, String condition, int syncType) {
		this.tableName = tableName;
		this.sql = COMMON_SQL.replace("$", tableName).replace("&", condition);
		this.primary = primary;
		this.syncType = syncType;
	}

	/**
	 * 关联表的构造
	 * 
	 * @param tableName
	 * @param primary
	 * @param colum
	 */
	public SyncBean(String tableName, String colum, String primary) {
		this.tableName = tableName;
		this.colum = colum;
		this.primary = primary;
		StringBuffer sql = new StringBuffer(NOT_COMMON_SQL.replace("$", tableName));
		sql.append(colum).append("=?");
		this.sql = sql.toString();
	}

	private List<SyncBean> relations;
	private String tableName;
	private String primary;
	private String colum;
	private String sql;
	private int syncType;
	private OrderBy orderby;

	public String getTableName() {
		return tableName;
	}

	public String getPrimary() {
		return primary;
	}

	public String getSql() {
		return sql;
	}

	public int getSyncType() {
		return syncType;
	}

	public String getColum() {
		return this.colum;
	}

	public List<SyncBean> getRelations() {
		return relations;
	}

	public void setRelations(List<SyncBean> relations) {
		this.relations = relations;
	}

	public OrderBy getOrderby() {
		return orderby;
	}

	static class OrderBy {

		public OrderBy(String column, String mode) {
			this.column = column;
			this.mode = mode;
		}

		private String column;
		private String mode;

		public String getColumn() {
			return column;
		}

		public String getMode() {
			return mode;
		}

	}
}
